package com.sxm.yiwei.activitis;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.Toast;

import com.alipay.sdk.app.PayTask;
import com.sxm.yiwei.MainActivity;
import com.sxm.yiwei.R;
import com.sxm.yiwei.demo.PayResult;
import com.sxm.yiwei.tools.BookPayActivity;
import com.sxm.yiwei.utils.InternetAddressUtis;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.Map;

public class MineNotification extends BaseActivity implements View.OnClickListener{

    private ImageView minentfback;
    private SharedPreferences sharedPreferences;
    private String token;
    private String userId;
    private String appId;
    private int no =1;
    private WebView noticeWeb;
    private ImageView backmain;
    private String data;
    private static final int SDK_PAY_FLAG = 1;

    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            PayResult payresult = new PayResult((Map<String, String>) msg.obj);
            String resultStatus = payresult.getResultStatus();
            if (resultStatus.equals("9000")) {
                Toast.makeText(MineNotification.this, "支付成功", Toast.LENGTH_SHORT).show();
            } else if (resultStatus.equals("8000")) {
                Toast.makeText(MineNotification.this, "支付处理中", Toast.LENGTH_SHORT).show();

            } else if (resultStatus.equals("4000")) {
                Toast.makeText(MineNotification.this, "支付失败", Toast.LENGTH_SHORT).show();

            } else if (resultStatus.equals("5000")) {
                Toast.makeText(MineNotification.this, "订单重复提交", Toast.LENGTH_SHORT).show();

            } else if (resultStatus.equals("6002")) {
                Toast.makeText(MineNotification.this, "请检查你的网络是否启", Toast.LENGTH_SHORT).show();

            } else {
                Toast.makeText(MineNotification.this, "请稍等，支付结果获取中", Toast.LENGTH_SHORT).show();

            }
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mine_notification);
        sharedPreferences = getSharedPreferences("userlogin", Context.MODE_PRIVATE);
        token = sharedPreferences.getString("token", "");
        userId = sharedPreferences.getString("userId", "");
        appId = sharedPreferences.getString("appid", "");

        initMineNotification();
        noticeWeb.getSettings().setJavaScriptEnabled(true);
        noticeWeb.loadUrl(InternetAddressUtis.Url+InternetAddressUtis.XCITYPARTNER);
        noticeWeb.addJavascriptInterface(new JavascriptInterface(),"jsInterface");

    }



    private void initMineNotification() {
        minentfback = (ImageView) findViewById(R.id.mine_ntf_back);
        noticeWeb = (WebView) findViewById(R.id.notice_web);
        backmain = (ImageView) findViewById(R.id.mine_notification_backmain);
        backmain.setOnClickListener(this);
        minentfback.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.mine_ntf_back:
                finish();
                break;
            case R.id.mine_notification_backmain:
                startActivity(new Intent(MineNotification.this, MainActivity.class));
                AppManager.getAppManager().finishAllActivity();
                break;
        }

    }


    class JavascriptInterface {

        @SuppressWarnings("unused")
        @android.webkit.JavascriptInterface /** 解决Android 17（包括17）之后js无法调用Android方法*/
        public void invokeMethod(String json) {
            // Toast.makeText(MainActivity.this, json, Toast.LENGTH_LONG).show();
            try {
                // 解析js传递过来的json串
                JSONObject mJson = new JSONObject(json);
                data = mJson.optString("data");
                Log.d("tag", "invokeMethod: 支付订单--"+data);
                final Runnable runnable = new Runnable() {
                    @Override
                    public void run() {
                        PayTask payTask = new PayTask(MineNotification.this);
                        Map<String, String> map = payTask.payV2(data, true);
                        Message msg = new Message();
                        msg.what = SDK_PAY_FLAG;
                        msg.obj = map;
                        mHandler.sendMessage(msg);
                    }
                };
                Thread payThread = new Thread(runnable);
                payThread.start();

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }
}
